<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术 | 冰河技术</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script charset="utf-8" async="async" src="/js/jquery.min.js"></script>
    <script charset="utf-8" async="async" src="/js/global.js"></script>
    <script charset="utf-8" async="async" src="/js/fingerprint2.min.js"></script>
    <script charset="utf-8" async="async" src="https://v1.cnzz.com/z_stat.php?id=1281063564&amp;web_id=1281063564"></script>
    <script charset="utf-8" async="async" src="https://s9.cnzz.com/z_stat.php?id=1281064551&amp;web_id=1281064551"></script>
    <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?d091d2fd0231588b1d0f9231e24e3f5e";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
            </script>
    <meta name="description" content="包含：编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧...">
    <meta property="article:modified_time" content="2022-05-23T11:30:51.000Z">
    <meta property="og:title" content="SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术">
    <meta property="og:type" content="article">
    <meta property="og:url" content="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html">
    <meta name="twitter:title" content="SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术">
    <meta name="twitter:url" content="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="robots" content="all">
    <meta name="author" content="冰河">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <meta name="keywords" content="冰河，冰河技术, 编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧">
    <meta name="apple-mobile-web-app-capable" content="yes">
    
    <link rel="preload" href="/assets/css/0.styles.ab888ebb.css" as="style"><link rel="preload" href="/assets/css/styles.css?v=1653305936337" as="style"><link rel="preload" href="/assets/js/cg-styles.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-app.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-4.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-3.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-302.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-5.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-6.js?v=1653305936337" as="script">
    <link rel="stylesheet" href="/assets/css/0.styles.ab888ebb.css"><link rel="stylesheet" href="/assets/css/styles.css?v=1653305936337">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">冰河技术</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>SpringBoot</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/microservices/springboot/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>SpringCloudAlibaba</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》专栏开篇啦</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-04-SA实战·第一篇-专栏设计.html" class="sidebar-link">SA实战 · 《SpringCloud Alibaba实战》专栏设计</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-09-SA实战-微服务介绍.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》微服务介绍</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-13-SA实战·项目说明-流程设计-技术选型-模块划分.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》项目说明、流程设计、技术选型与模块划分</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-18-SA实战-开撸-完成通用模块的开发.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》开撸：完成通用模块的开发</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-21-SA实战-完成三大微服务的搭建与交互开发.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》快速搭建三大微服务并完成交互开发与测试</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA实战-服务治理-实现服务的注册与发现.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-27-SA实战-第8章-服务治理-实现服务调用的负载均衡.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第8章-服务治理：实现服务调用的负载均衡</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-28-SA实战-第9章-服务容错-服务雪崩与容错方案.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第9章-服务容错：服务雪崩与容错方案</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-03-SA实战-第10章-服务容错-Fegin整合Sentinel.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第10章-服务容错：项目整合Sentinel实现限流与容错</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-05-SA实战-第11章-服务容错加餐-Sentinel核心技术与配置规则.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第11章-服务容错加餐：Sentinel核心技术与配置规则（最全使用教程）</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-08-SA实战-第12章-服务网关-网关概述与核心架构.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第12章-服务网关：网关概述与核心架构</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-08-SA实战-第13章-服务网关-项目整合SpringCloudGateway.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第13章-服务网关：项目整合SpringCloud Gateway网关</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA实战-第14章-服务网关-SpringCloudGateway核心技术.html" class="active sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-11-SA实战-第15章-链路追踪-核心原理与解决方案.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第15章-链路追踪：核心原理与解决方案</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-12-SA实战-第16章-链路追踪-项目整合Sleuth实现链路追踪.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第16章-链路追踪：项目整合Sleuth实现链路追踪</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-13-SA实战-第17章-链路追踪-Sleuth整合ZipKin.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第17章-链路追踪：Sleuth整合ZipKin</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-17-SA实战-第18章-消息服务-MQ使用场景与选型对比.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第18章-消息服务：MQ使用场景与选型对比</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-18-SA实战-第19章-消息服务-项目整合RocketMQ.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第19章-消息服务：项目整合RocketMQ</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-20-SA实战-第20章-消息服务-RocketMQ核心技术.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第20章-消息服务：RocketMQ核心技术</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-21-SA实战-第21章-服务配置-服务配置介绍与Nacos核心概念.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第21章-服务配置：服务配置介绍与Nacos核心概念</a></li></ul></section></li></ul> </aside> <div><main class="page"> <div class="theme-default-content content__default"><h1 id="sa实战-·《springcloud-alibaba实战》第14章-服务网关加餐-springcloud-gateway核心技术"><a href="#sa实战-·《springcloud-alibaba实战》第14章-服务网关加餐-springcloud-gateway核心技术" class="header-anchor">#</a> SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术</h1> <p><strong>大家好，我是冰河~~</strong></p> <blockquote><p>一不小心《<a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg4MjU0OTM1OA==&amp;action=getalbum&amp;album_id=2337104419664084992&amp;scene=173&amp;from_msgid=2247500408&amp;from_itemidx=1&amp;count=3&amp;nolastread=1#wechat_redirect" target="_blank" rel="noopener noreferrer">SpringCloud Alibaba实战<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》专栏都更新到第14章了，再不上车就跟不上了，小伙伴们快跟上啊！</p> <p>注意：本项目完整源码加入 <strong><a href="https://public.zsxq.com/groups/48848484411888.html" target="_blank" rel="noopener noreferrer">冰河技术<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></strong> 知识星球即可获取，文末有优惠券。</p></blockquote> <p>在《<a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg4MjU0OTM1OA==&amp;action=getalbum&amp;album_id=2337104419664084992&amp;scene=173&amp;from_msgid=2247500408&amp;from_itemidx=1&amp;count=3&amp;nolastread=1#wechat_redirect" target="_blank" rel="noopener noreferrer">SpringCloud Alibaba实战<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》专栏前面的文章中，我们实现了用户微服务、商品微服务和订单微服务之间的远程调用，并且实现了服务调用的负载均衡。也基于阿里开源的Sentinel实现了服务的限流与容错，并详细介绍了Sentinel的核心技术与配置规则，同时，简单介绍了服务网关，并对SpringCloud Gateway的核心架构进行了简要说明，也在项目中整合了SpringCloud Gateway网关实现了通过网关访问后端微服务，另外，也能够SpringCloud Gateway整合Sentinel实现了网关的限流功能。今天，我们再进一步介绍下SpringCloud Gateway网关的核心技术。</p> <h2 id="本章总览"><a href="#本章总览" class="header-anchor">#</a> 本章总览</h2> <p><img alt="sa-2022-05-10-013" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-013.png" loading="lazy" class="lazy"></p> <h2 id="本章概述"><a href="#本章概述" class="header-anchor">#</a> 本章概述</h2> <p>SpringCloud Gateway能够实现多种网关功能，比如路由转发、断言、过滤器、熔断、限流、降级、自定义谓词配置、自定义过滤器等等多种功能。今天，我们就一起来聊聊SpringCloud Gateway中的断言、过滤器与熔断机制。</p> <h2 id="网关断言"><a href="#网关断言" class="header-anchor">#</a> 网关断言</h2> <p>断言的英文是Predicate，也可以翻译成谓词。主要的作用就是进行条件判断，可以在网关中实现多种条件判断，只有所有的判断结果都通过时，也就是所有的条件判断都返回true，才会真正的执行路由功能。</p> <h3 id="springcloud-gateway内置断言"><a href="#springcloud-gateway内置断言" class="header-anchor">#</a> SpringCloud Gateway内置断言</h3> <p>SpringCloud Gateway包括许多内置的断言工厂，所有这些断言都与HTTP请求的不同属性匹配。</p> <h4 id="基于日期时间类型的断言"><a href="#基于日期时间类型的断言" class="header-anchor">#</a> 基于日期时间类型的断言</h4> <p>基于日期时间类型的断言根据时间做判断，主要有三个：</p> <ul><li>AfterRoutePredicateFactory： 接收一个日期参数，判断请求日期是否晚于指定日期</li> <li>BeforeRoutePredicateFactory： 接收一个日期参数，判断请求日期是否早于指定日期</li> <li>BetweenRoutePredicateFactory： 接收两个日期参数，判断请求日期是否在指定时间段内</li></ul> <h4 id="使用示例"><a href="#使用示例" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">After</span><span class="token operator">=</span><span class="token number">2022</span>-05-10T23:59:59.256+08:00<span class="token punctuation">[</span>Asia/Shanghai<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于远程地址的断言"><a href="#基于远程地址的断言" class="header-anchor">#</a> 基于远程地址的断言</h4> <p>RemoteAddrRoutePredicateFactory：接收一个IP地址段，判断请求主机地址是否在地址段中。</p> <h4 id="使用示例-2"><a href="#使用示例-2" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">RemoteAddr</span><span class="token operator">=</span><span class="token number">192.168</span>.0.1/24
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于cookie的断言"><a href="#基于cookie的断言" class="header-anchor">#</a> 基于Cookie的断言</h4> <p>CookieRoutePredicateFactory：接收两个参数， cookie 名字和一个正则表达式。 判断请求cookie是否具有给定名称且值与正则表达式匹配。</p> <h4 id="使用示例-3"><a href="#使用示例-3" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">Cookie</span><span class="token operator">=</span>name, binghe.
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于header的断言"><a href="#基于header的断言" class="header-anchor">#</a> 基于Header的断言</h4> <p>HeaderRoutePredicateFactory：接收两个参数，标题名称和正则表达式。 判断请求Header是否具有给定名称且值与正则表达式匹配。</p> <h4 id="使用示例-4"><a href="#使用示例-4" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">Header</span><span class="token operator">=</span>X-Request-Id, <span class="token punctuation">\</span>d+
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于host的断言"><a href="#基于host的断言" class="header-anchor">#</a> 基于Host的断言</h4> <p>HostRoutePredicateFactory：接收一个参数，主机名模式。判断请求的Host是否满足匹配规则。</p> <h4 id="使用示例-5"><a href="#使用示例-5" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">Host</span><span class="token operator">=</span>**.binghe.com
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于method请求方法的断言"><a href="#基于method请求方法的断言" class="header-anchor">#</a> 基于Method请求方法的断言</h4> <p>MethodRoutePredicateFactory：接收一个参数，判断请求类型是否跟指定的类型匹配。</p> <h4 id="使用示例-6"><a href="#使用示例-6" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">Method</span><span class="token operator">=</span>GET
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于path请求路径的断言"><a href="#基于path请求路径的断言" class="header-anchor">#</a> 基于Path请求路径的断言</h4> <p>PathRoutePredicateFactory：接收一个参数，判断请求的URI部分是否满足路径规则。</p> <h4 id="使用示例-7"><a href="#使用示例-7" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">Path</span><span class="token operator">=</span>/binghe/<span class="token punctuation">{</span>segment<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于query请求参数的断言"><a href="#基于query请求参数的断言" class="header-anchor">#</a> 基于Query请求参数的断言</h4> <p>QueryRoutePredicateFactory ：接收两个参数，请求参数和正则表达式， 判断请求参数是否具有给定名称且值与正则表达式匹配。</p> <h4 id="使用示例-8"><a href="#使用示例-8" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">Query</span><span class="token operator">=</span>name, binghe.
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="基于路由权重的断言"><a href="#基于路由权重的断言" class="header-anchor">#</a> 基于路由权重的断言</h4> <p>WeightRoutePredicateFactory：接收一个[组名,权重], 然后对于同一个组内的路由按照权重转发。</p> <h4 id="使用示例-9"><a href="#使用示例-9" class="header-anchor">#</a> 使用示例</h4> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- id: weight1
  uri: http://localhost:8080
  predicates:
    - <span class="token assign-left variable">Path</span><span class="token operator">=</span>/api/**
    - <span class="token assign-left variable">Weight</span><span class="token operator">=</span>group1,2
  filters:
    - <span class="token assign-left variable">StripPrefix</span><span class="token operator">=</span><span class="token number">1</span>
- id: weight2
  uri: http://localhost:8081
  predicates:
    - <span class="token assign-left variable">Path</span><span class="token operator">=</span>/api/**
    - <span class="token assign-left variable">Weight</span><span class="token operator">=</span>group1,8
  filters:
    - <span class="token assign-left variable">StripPrefix</span><span class="token operator">=</span><span class="token number">1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="演示内置断言"><a href="#演示内置断言" class="header-anchor">#</a> 演示内置断言</h3> <p>在演示的示例中，我们基于Path请求路径的断言判断请求路径是否符合规则，基于远程地址的断言判断请求主机地址是否在地址段中，并且限制请求的方式为GET方式。整个演示的过程以访问用户微服务的接口为例。</p> <p>（1）由于在开发项目时，所有的服务都是在我本地启动的，首先查看下我本机的IP地址，如下所示。</p> <p><img alt="sa-2022-05-10-001" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-001.png" loading="lazy" class="lazy"></p> <p>可以看到，我本机的IP地址为192.168.0.27，属于192.168.0.1/24网段。</p> <p>（2）在服务网关模块shop-gateway中，将application.yml文件备份成application-sentinel.yml文件，并将application.yml文件中的内容修改成application-simple.yml文件中的内容。接下来，在application.yml文件中的<code>spring.cloud.gateway.routes</code>节点下的<code>- id: user-gateway</code>下面进行断言配置，配置后的结果如下所示。</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> user<span class="token punctuation">-</span>gateway
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8060</span>
          <span class="token key atrule">order</span><span class="token punctuation">:</span> <span class="token number">1</span>
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/server<span class="token punctuation">-</span>user/<span class="token important">**</span>
            <span class="token punctuation">-</span> RemoteAddr=192.168.0.1/24
            <span class="token punctuation">-</span> Method=GET
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p><strong>注意：完整的配置参见案例完整源代码。</strong></p> <p>（3）配置完成后启动用户微服务和网关服务，通过网关服务访问用户微服务，在浏览器中输入<code>http://localhost:10001/server-user/user/get/1001</code>，如下所示。</p> <p><img alt="sa-2022-05-10-002" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-002.png" loading="lazy" class="lazy"></p> <p>可以看到通过<code>http://localhost:10001/server-user/user/get/1001</code>链接不能正确访问到用户信息。</p> <p>接下来，在浏览器中输入<code>http://192.168.0.27:10001/server-user/user/get/1001</code>，能够正确获取到用户的信息。</p> <p><img alt="sa-2022-05-10-003" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-003.png" loading="lazy" class="lazy"></p> <p>（4）停止网关微服务，将基于远程地址的断言配置成<code>- RemoteAddr=192.168.1.1/24</code>，也就是将基于远程地址的断言配置成与我本机IP地址不在同一个网段，这样就能演示请求主机地址不在地址段中的情况，修改后的基于远程地址的断言配置如下所示。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>- <span class="token assign-left variable">RemoteAddr</span><span class="token operator">=</span><span class="token number">192.168</span>.1.1/24
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>（5）重启网关服务，再次在浏览器中输入<code>http://localhost:10001/server-user/user/get/1001</code>，如下所示。</p> <p><img alt="sa-2022-05-10-004" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-004.png" loading="lazy" class="lazy"></p> <p>可以看到通过<code>http://localhost:10001/server-user/user/get/1001</code>链接不能正确访问到用户信息。</p> <p>接下来，在浏览器中输入<code>http://192.168.0.27:10001/server-user/user/get/1001</code>，也不能正确获取到用户的信息了。</p> <p><img alt="sa-2022-05-10-005" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-005.png" loading="lazy" class="lazy"></p> <h3 id="自定义断言"><a href="#自定义断言" class="header-anchor">#</a> 自定义断言</h3> <p>SpringCloud Gateway支持自定义断言功能，我们可以在具体业务中，基于SpringCloud Gateway自定义特定的断言功能。</p> <h4 id="自定义断言概述"><a href="#自定义断言概述" class="header-anchor">#</a> 自定义断言概述</h4> <p>SpringCloud Gateway虽然提供了多种内置的断言功能，但是在某些场景下无法满足业务的需要，此时，我们就可以基于SpringCloud Gateway自定义断言功能，以此来满足我们的业务场景。</p> <h4 id="实现自定义断言"><a href="#实现自定义断言" class="header-anchor">#</a> 实现自定义断言</h4> <p>这里，我们基于SpringCloud Gateway实现断言功能，实现后的效果是在服务网关的application.yml文件中的<code>spring.cloud.gateway.routes</code>节点下的<code>- id: user-gateway</code>下面进行如下配置。</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> user<span class="token punctuation">-</span>gateway
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8060</span>
          <span class="token key atrule">order</span><span class="token punctuation">:</span> <span class="token number">1</span>
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/server<span class="token punctuation">-</span>user/<span class="token important">**</span>
            <span class="token punctuation">-</span> Name=binghe
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>通过服务网关访问用户微服务时，只有在访问的链接后面添加<code>?name=binghe</code>参数时才能正确访问用户微服务。</p> <p>（1）在网关服务shop-gateway中新建<code>io.binghe.shop.predicate</code>包，在包下新建NameRoutePredicateConfig类，主要定义一个Spring类型的name成员变量，用来接收配置文件中的参数，源码如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 接收配置文件中的参数
 */</span>
<span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NameRoutePredicateConfig</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">3289515863427972825L</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>（2）实现自定义断言时，需要新建类继承<code>org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory</code>类，在<code>io.binghe.shop.predicate</code>包下新建NameRoutePredicateFactory类，继承<code>org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory</code>类，并覆写相关的方法，源码如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 自定义断言功能
 */</span>
<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NameRoutePredicateFactory</span> <span class="token keyword">extends</span> <span class="token class-name">AbstractRoutePredicateFactory</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">NameRoutePredicateConfig</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token class-name">NameRoutePredicateFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token class-name">NameRoutePredicateConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Predicate</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ServerWebExchange</span><span class="token punctuation">&gt;</span></span> <span class="token function">apply</span><span class="token punctuation">(</span><span class="token class-name">NameRoutePredicateConfig</span> config<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span>serverWebExchange<span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token punctuation">{</span>
            <span class="token class-name">String</span> name <span class="token operator">=</span> serverWebExchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getQueryParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getFirst</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                name <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> name<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>config<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> <span class="token function">shortcutFieldOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>（3）在服务网关的application.yml文件中的<code>spring.cloud.gateway.routes</code>节点下的<code>- id: user-gateway</code>下面进行如下配置。</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> user<span class="token punctuation">-</span>gateway
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8060</span>
          <span class="token key atrule">order</span><span class="token punctuation">:</span> <span class="token number">1</span>
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/server<span class="token punctuation">-</span>user/<span class="token important">**</span>
            <span class="token punctuation">-</span> Name=binghe
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>（4）分别启动用户微服务与网关服务，在浏览器中输入<code>http://localhost:10001/server-user/user/get/1001</code>，如下所示。</p> <p><img alt="sa-2022-05-10-006" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-006.png" loading="lazy" class="lazy"></p> <p>可以看到，在浏览器中输入<code>http://localhost:10001/server-user/user/get/1001</code>，无法获取到用户信息。</p> <p>（5）在浏览器中输入<code>http://localhost:10001/server-user/user/get/1001?name=binghe</code>，如下所示。</p> <p><img alt="sa-2022-05-10-007" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-007.png" loading="lazy" class="lazy"></p> <p>可以看到，在访问链接后添加<code>?name=binghe</code>参数后，能够正确获取到用户信息。</p> <p>至此，我们实现了自定义断言功能。</p> <h2 id="网关过滤器"><a href="#网关过滤器" class="header-anchor">#</a> 网关过滤器</h2> <p>过滤器可以在请求过程中，修改请求的参数和响应的结果等信息。在生命周期的角度总体上可以分为前置过滤器（Pre）和后置过滤器(Post)。在实现的过滤范围角度可以分为局部过滤器（GatewayFilter）和全局过滤器（GlobalFilter）。局部过滤器作用的范围是某一个路由，全局过滤器作用的范围是全部路由。</p> <ul><li>Pre前置过滤器：在请求被网关路由之前调用，可以利用这种过滤器实现认证、鉴权、路由等功能，也可以记录访问时间等信息。</li> <li>Post后置过滤器：在请求被网关路由到微服务之后执行。可以利用这种过滤器修改HTTP的响应Header信息，修改返回的结果数据（例如对于一些敏感的数据，可以在此过滤器中统一处理后返回），收集一些统计信息等。</li> <li>局部过滤器（GatewayFilter）：也可以称为网关过滤器，这种过滤器主要是作用于单一路由或者某个路由分组。</li> <li>全局过滤器（GlobalFilter）：这种过滤器主要作用于所有的路由。</li></ul> <h3 id="局部过滤器"><a href="#局部过滤器" class="header-anchor">#</a> 局部过滤器</h3> <p>局部过滤器又称为网关过滤器，这种过滤器主要是作用于单一路由或者某个路由分组。</p> <h4 id="局部过滤器概述"><a href="#局部过滤器概述" class="header-anchor">#</a> 局部过滤器概述</h4> <p>在SpringCloud Gateway中内置了很多不同类型的局部过滤器，主要如下所示。</p> <table><thead><tr><th>过滤器</th> <th>作用</th> <th>参数</th></tr></thead> <tbody><tr><td>AddRequestHeader</td> <td>为原始请求添加Header</td> <td>Header的名称及值</td></tr> <tr><td>AddRequestParameter</td> <td>为原始请求添加请求参数</td> <td>参数名称及值</td></tr> <tr><td>AddResponseHeader</td> <td>为原始响应添加Header</td> <td>Header的名称及值</td></tr> <tr><td>DedupeResponseHeader</td> <td>剔除响应头中重复的值</td> <td>需要去重的Header名 称及去重策略</td></tr> <tr><td>Hystrix</td> <td>为路由引入Hystrix的断路器保护</td> <td>HystrixCommand的名 称</td></tr> <tr><td>FallbackHeaders</td> <td>为fallbackUri的请求头中添加具 体的异常信息</td> <td>Header的名称</td></tr> <tr><td>PrefixPath</td> <td>为原始请求路径添加前缀</td> <td>前缀路径</td></tr> <tr><td>PreserveHostHeader</td> <td>为请求添加一个 preserveHostHeader=true的属 性， 路由过滤器会检查该属性以 决定是否要发送原始的Host</td> <td>无</td></tr> <tr><td>RequestRateLimiter</td> <td>用于对请求限流， 限流算法为令 牌桶</td> <td>keyResolver、 rateLimiter、 statusCode、 denyEmptyKey、 emptyKeyStatus</td></tr> <tr><td>RedirectTo</td> <td>将原始请求重定向到指定的URL</td> <td>http状态码及重定向的 url</td></tr> <tr><td>RemoveHopByHopHeadersFilter</td> <td>为原始请求删除IETF组织规定的 一系列Header</td> <td>默认就会启用， 可以通 过配置指定仅删除哪些 Header</td></tr> <tr><td>RemoveRequestHeader</td> <td>为原始请求删除某个Header</td> <td>Header名称</td></tr> <tr><td>RemoveResponseHeader</td> <td>为原始响应删除某个Header</td> <td>Header名称</td></tr> <tr><td>RewritePath</td> <td>重写原始的请求路径</td> <td>原始路径正则表达式以 及重写后路径的正则表 达式</td></tr> <tr><td>RewriteResponseHeader</td> <td>重写原始响应中的某个Header</td> <td>Header名称， 值的正 则表达式， 重写后的值</td></tr> <tr><td>SaveSession</td> <td>在转发请求之前， 强制执行 WebSession::save操作</td> <td>无</td></tr> <tr><td>secureHeaders</td> <td>为原始响应添加一系列起安全作 用的响应头</td> <td>无， 支持修改这些安全 响应头的值</td></tr> <tr><td>SetPath</td> <td>修改原始的请求路径</td> <td>修改后的路径</td></tr> <tr><td>SetResponseHeader</td> <td>修改原始响应中某个Header的值</td> <td>Header名称， 修改后 的值</td></tr> <tr><td>SetStatus</td> <td>修改原始响应的状态码</td> <td>HTTP 状态码， 可以是 数字， 也可以是字符串</td></tr> <tr><td>StripPrefix</td> <td>用于截断原始请求的路径</td> <td>使用数字表示要截断的 路径的数量</td></tr> <tr><td>Retry</td> <td>针对不同的响应进行重试</td> <td>retries、 statuses、 methods、 series</td></tr> <tr><td>RequestSize</td> <td>设置允许接收最大请求包的大 小。 如果请求包大小超过设置的 值， 则返回 413 Payload Too Large</td> <td>请求包大小， 单位为字 节， 默认值为5M</td></tr> <tr><td>ModifyRequestBody</td> <td>在转发请求之前修改原始请求体 内容</td> <td>修改后的请求体内容</td></tr> <tr><td>ModifyResponseBody</td> <td>修改原始响应体的内容</td> <td>修改后的响应体内容</td></tr></tbody></table> <p align="right"><font size="2">注：表格转自互联网。</font></p> <h4 id="演示内部过滤器"><a href="#演示内部过滤器" class="header-anchor">#</a> 演示内部过滤器</h4> <p>演示内部过滤器时，我们为原始请求添加一个名称为IP的Header，值为localhost，并添加一个名称为name的参数，参数值为binghe。同时修改响应的结果状态，将结果状态修改为1001。</p> <p>（1）在服务网关的application.yml文件中的<code>spring.cloud.gateway.routes</code>节点下的<code>- id: user-gateway</code>下面进行如下配置。</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> user<span class="token punctuation">-</span>gateway
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8060</span>
          <span class="token key atrule">order</span><span class="token punctuation">:</span> <span class="token number">1</span>
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/server<span class="token punctuation">-</span>user/<span class="token important">**</span>
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
            <span class="token punctuation">-</span> AddRequestHeader=IP<span class="token punctuation">,</span>localhost
            <span class="token punctuation">-</span> AddRequestParameter=name<span class="token punctuation">,</span>binghe
            <span class="token punctuation">-</span> SetStatus=1001
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>（2）在用户微服务的<code>io.binghe.shop.user.controller.UserController</code>类中新增apiFilter1()方法，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">&quot;/api/filter1&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">apiFilter1</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span><span class="token punctuation">{</span>
    log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问了apiFilter1接口&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> ip <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getHeader</span><span class="token punctuation">(</span><span class="token string">&quot;IP&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> name <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;ip = &quot;</span> <span class="token operator">+</span> ip <span class="token operator">+</span> <span class="token string">&quot;, name = &quot;</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">&quot;apiFilter1&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>可以看到，在新增加的apiFilter1()方法中，获取到新增加的Header与参数，并将获取出来的参数与Header打印出来。并且方法返回的是字符串apiFilter1。</p> <p>（3）分别启动用户微服务与网关服务，在浏览器中输入<code>http://localhost:10001/server-user/user/api/filter1</code>，如下所示。</p> <p><img alt="sa-2022-05-10-008" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-008.png" loading="lazy" class="lazy"></p> <p>此时，查看浏览器中的响应状态码，如下所示。</p> <p><img alt="sa-2022-05-10-009" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-009.png" loading="lazy" class="lazy"></p> <p>可以看到，此时的状态码已经被修改为1001。</p> <p>接下来，查看下用户微服务的控制台输出的信息，发现在输出的信息中存在如下数据。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>访问了apiFilter1接口
<span class="token function">ip</span> <span class="token operator">=</span> localhost, name <span class="token operator">=</span> binghe
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>说明使用SpringCloud Gateway的内置过滤器成功为原始请求添加了一个名称为IP的Header，值为localhost，并添加了一个名称为name的参数，参数值为binghe。同时修改了响应的结果状态，将结果状态修改为1001，符合预期效果。</p> <h4 id="自定义局部过滤器"><a href="#自定义局部过滤器" class="header-anchor">#</a> 自定义局部过滤器</h4> <p>这里，我们基于SpringCloud Gateway自定义局部过滤器实现是否开启灰度发布的功能，整个实现过程如下所示。</p> <p>（1）在服务网关的application.yml文件中的<code>spring.cloud.gateway.routes</code>节点下的<code>- id: user-gateway</code>下面进行如下配置。</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> user<span class="token punctuation">-</span>gateway
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8060</span>
          <span class="token key atrule">order</span><span class="token punctuation">:</span> <span class="token number">1</span>
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/server<span class="token punctuation">-</span>user/<span class="token important">**</span>
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
            <span class="token punctuation">-</span> Grayscale=true
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>（2）在网关服务模块shop-gateway中新建<code>io.binghe.shop.filter</code>包，在包下新建GrayscaleGatewayFilterConfig类，用于接收配置中的参数，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 接收配置参数
 */</span>
<span class="token annotation punctuation">@Data</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GrayscaleGatewayFilterConfig</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token number">983019309000445082L</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> grayscale<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>（3）在<code>io.binghe.shop.filter</code>包下GrayscaleGatewayFilterFactory类，继承<code>org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory</code>类，主要是实现自定义过滤器，模拟实现灰度发布。代码如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 自定义过滤器模拟实现灰度发布
 */</span>
<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GrayscaleGatewayFilterFactory</span> <span class="token keyword">extends</span> <span class="token class-name">AbstractGatewayFilterFactory</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">GrayscaleGatewayFilterConfig</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token class-name">GrayscaleGatewayFilterFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token class-name">GrayscaleGatewayFilterConfig</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">GatewayFilter</span> <span class="token function">apply</span><span class="token punctuation">(</span><span class="token class-name">GrayscaleGatewayFilterConfig</span> config<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span>exchange<span class="token punctuation">,</span> chain<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>config<span class="token punctuation">.</span><span class="token function">isGrayscale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;开启了灰度发布功能...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;关闭了灰度发布功能...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> <span class="token function">shortcutFieldOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">&quot;grayscale&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>（4）分别启动用户微服务和服务网关，在浏览器中输入<code>http://localhost:10001/server-user/user/get/1001</code>，如下所示。</p> <p><img alt="sa-2022-05-10-010" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-010.png" loading="lazy" class="lazy"></p> <p>可以看到，通过服务网关正确访问到了用户微服务，并正确获取到了用户信息。</p> <p>接下来，查看下服务网关的终端，发现已经成功输出了如下信息。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>开启了灰度发布功能<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>说明正确实现了自定义的局部过滤器。</p> <h3 id="全局过滤器"><a href="#全局过滤器" class="header-anchor">#</a> 全局过滤器</h3> <p>全局过滤器是一系列特殊的过滤器，会根据条件应用到所有路由中。</p> <h4 id="全局过滤器概述"><a href="#全局过滤器概述" class="header-anchor">#</a> 全局过滤器概述</h4> <p>在SpringCloud Gateway中内置了多种不同的全局过滤器，如下所示。</p> <table><thead><tr><th>过滤器</th> <th>作用</th></tr></thead> <tbody><tr><td>ForwardRoutingFilter</td> <td>用于本地forward，也就是将请求在Gateway服务内进行转发，而不是转发到下游服务。</td></tr> <tr><td>LoadBalancerClientFilter</td> <td>整合Ribbon实现负载均衡。</td></tr> <tr><td>NettyRoutingFilter</td> <td>使用Netty的HttpClient 转发http、https请求。</td></tr> <tr><td>NettyWriteResponseFilter</td> <td>将代理响应写回网关的客户端侧。</td></tr> <tr><td>RouteToRequestUrlFilter</td> <td>将从request里获取的原始url转换成Gateway进行请求转发时所使用的url。</td></tr> <tr><td>WebsocketRoutingFilter</td> <td>使用Spring Web Socket将转发 Websocket 请求。</td></tr> <tr><td>GatewayMetricsFilter</td> <td>整合监控相关，提供监控指标。</td></tr> <tr><td>ForwardPathFilter</td> <td>解析路径，并转发路径。</td></tr> <tr><td>WebClientHttpRoutingFilter</td> <td>通过WebClient客户端转发请求真实的URL。</td></tr> <tr><td>WebClientWriteResponseFilter</td> <td>将响应信息写入到当前的请求响应中。</td></tr></tbody></table> <h4 id="演示全局过滤器"><a href="#演示全局过滤器" class="header-anchor">#</a> 演示全局过滤器</h4> <p>（1）在服务网关模块shop-gateway模块下的<code>io.binghe.shop.config</code>包下新建GatewayFilterConfig类，并在类中配置几个全局过滤器，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 网关过滤器配置
 */</span>
<span class="token annotation punctuation">@Configuration</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GatewayFilterConfig</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token annotation punctuation">@Order</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">GlobalFilter</span> <span class="token function">globalFilter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span>exchange<span class="token punctuation">,</span> chain<span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;执行前置过滤器逻辑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token class-name">Mono</span><span class="token punctuation">.</span><span class="token function">fromRunnable</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>
                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;执行后置过滤器逻辑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p><strong>注意：@Order注解中的数字越小，执行的优先级越高。</strong></p> <p>（2）启动用户微服务与服务网关，在浏览器中访问<code>http://localhost:10001/server-user/user/get/1001</code>，如下所示。</p> <p><img alt="sa-2022-05-10-010" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-010.png" loading="lazy" class="lazy"></p> <p>在服务网关终端输出如下信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>执行前置过滤器逻辑
执行后置过滤器逻辑
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>说明我们演示的全局过滤器生效了。</p> <h4 id="自定义全局过滤器"><a href="#自定义全局过滤器" class="header-anchor">#</a> 自定义全局过滤器</h4> <p>SpringCloud Gateway内置了很多全局过滤器，一般情况下能够满足实际开发需要，但是对于某些特殊的业务场景，还是需要我们自己实现自定义全局过滤器。</p> <p>这里，我们就模拟实现一个获取客户端访问信息，并统计访问接口时长的全局过滤器。</p> <p>（1）在网关服务模块shop-order的<code>io.binghe.shop.filter</code>包下，新建GlobalGatewayLogFilter类，实现<code>org.springframework.cloud.gateway.filter.GlobalFilter</code>接口和<code>org.springframework.core.Ordered</code>接口，代码如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 自定义全局过滤器，模拟实现获取客户端信息并统计接口访问时长
 */</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GlobalGatewayLogFilter</span> <span class="token keyword">implements</span> <span class="token class-name">GlobalFilter</span><span class="token punctuation">,</span> <span class="token class-name">Ordered</span> <span class="token punctuation">{</span>
    <span class="token comment">/**
     * 开始访问时间
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token class-name">String</span> BEGIN_VISIT_TIME <span class="token operator">=</span> <span class="token string">&quot;begin_visit_time&quot;</span><span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">&gt;</span></span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> exchange<span class="token punctuation">,</span> <span class="token class-name">GatewayFilterChain</span> chain<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//先记录下访问接口的开始时间</span>
        exchange<span class="token punctuation">.</span><span class="token function">getAttributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>BEGIN_VISIT_TIME<span class="token punctuation">,</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token class-name">Mono</span><span class="token punctuation">.</span><span class="token function">fromRunnable</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token punctuation">{</span>
            <span class="token class-name">Long</span> beginVisitTime <span class="token operator">=</span> exchange<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span>BEGIN_VISIT_TIME<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>beginVisitTime <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问接口主机: &quot;</span> <span class="token operator">+</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问接口端口: &quot;</span> <span class="token operator">+</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问接口URL: &quot;</span> <span class="token operator">+</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问接口URL参数: &quot;</span> <span class="token operator">+</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getURI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRawQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问接口时长: &quot;</span> <span class="token operator">+</span> <span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> beginVisitTime<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;ms&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p>上述代码的实现逻辑还是比较简单的，这里就不再赘述了。</p> <p>（2）启动用户微服务与网关服务，在浏览器中输入<code>http://localhost:10001/server-user/user/api/filter1?name=binghe</code>，如下所示。</p> <p><img alt="sa-2022-05-10-012" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-05-10-012.png" loading="lazy" class="lazy"></p> <p>接下来，查看服务网关的终端日志，可以发现已经输出了如下信息。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>访问接口主机: localhost
访问接口端口: <span class="token number">10001</span>
访问接口URL: /server-user/user/api/filter1
访问接口URL参数: <span class="token assign-left variable">name</span><span class="token operator">=</span>binghe
访问接口时长: 126ms
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>说明我们自定义的全局过滤器生效了。</p> <h2 id="网关熔断机制"><a href="#网关熔断机制" class="header-anchor">#</a> 网关熔断机制</h2> <p>其实熔断机制在《<a href="https://binghe001.github.io/md/microservices/springcloudalibaba/2022-05-08-SA%E5%AE%9E%E6%88%98-%E7%AC%AC13%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-%E9%A1%B9%E7%9B%AE%E6%95%B4%E5%90%88SpringCloudGateway.html" target="_blank" rel="noopener noreferrer">SA实战 ·《SpringCloud Alibaba实战》第13章-服务网关：项目整合SpringCloud Gateway网关<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》一文中就基于SpringCloud Gateway整合Sentinel实现了。大家可以参见《<a href="https://binghe001.github.io/md/microservices/springcloudalibaba/2022-05-08-SA%E5%AE%9E%E6%88%98-%E7%AC%AC13%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-%E9%A1%B9%E7%9B%AE%E6%95%B4%E5%90%88SpringCloudGateway.html" target="_blank" rel="noopener noreferrer">SA实战 ·《SpringCloud Alibaba实战》第13章-服务网关：项目整合SpringCloud Gateway网关<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》一文。</p> <p><strong>注意：整个实战案例基于SpringCloud Alibaba技术栈实现，所以，整个案例专栏也是偏向于使用SpringCloud Alibaba技术栈的。</strong></p> <p><strong>好了，今天我们就到儿吧，限于篇幅，文中并未给出完整的案例源代码，想要完整源代码的小伙伴可加入【冰河技术】知识星球获取源码。也可以加我微信：hacker_binghe，一起交流技术。</strong></p> <p><strong>另外，一不小心就写了14章了，小伙伴们你们再不上车就真的跟不上了！！！</strong></p> <h2 id="关于星球"><a href="#关于星球" class="header-anchor">#</a> 关于星球</h2> <p>最近，冰河创建了【冰河技术】知识星球，《<a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg4MjU0OTM1OA==&amp;action=getalbum&amp;album_id=2337104419664084992&amp;scene=173&amp;from_msgid=2247500214&amp;from_itemidx=1&amp;count=3&amp;nolastread=1#wechat_redirect" target="_blank" rel="noopener noreferrer">SpringCloud Alibaba实战<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》专栏的源码获取方式会放到知识星期中，同时在微信上会创建专门的知识星球群，冰河会在知识星球上和星球群里解答球友的提问。</p> <p>今天，【冰河技术】知识星球再开放200张优惠券，还没上车的小伙伴赶紧啦，再不上车就跟不上啦！！</p> <h3 id="星球提供的服务"><a href="#星球提供的服务" class="header-anchor">#</a> 星球提供的服务</h3> <p>冰河整理了星球提供的一些服务，如下所示。</p> <p>加入星球，你将获得：</p> <p>1.学习SpringCloud Alibaba实战项目—从零开发微服务项目</p> <p>2.学习高并发、大流量业务场景的解决方案，体验大厂真正的高并发、大流量的业务场景</p> <p>3.学习进大厂必备技能：性能调优、并发编程、分布式、微服务、框架源码、中间件开发、项目实战</p> <p>4.提供站点 https://binghe001.github.io 所有学习内容的指导、帮助</p> <p>5.GitHub：https://github.com/binghe001/BingheGuide - 非常有价值的技术资料仓库，包括冰河所有的博客开放案例代码</p> <p>6.可以发送你的简历到我的邮箱，提供简历批阅服务</p> <p>7.提供技术问题、系统架构、学习成长、晋升答辩等各项内容的回答</p> <p>8.定期的整理和分享出各类专属星球的技术小册、电子书、编程视频、PDF文件</p> <p>9.定期组织技术直播分享，传道、授业、解惑，指导阶段瓶颈突破技巧</p> <h3 id="星球门票价格"><a href="#星球门票价格" class="header-anchor">#</a> 星球门票价格</h3> <p>星球目前的门票价格50元，随着每次加入新实战项目和分享硬核技术上调入场价格。</p> <p>最后，小伙伴们可以扫描或者长按下图中的二维码加入星球，也可以在 <strong>冰河技术</strong> 公众号回复 “ <strong>星球</strong> ” ，领取入场优惠券。</p> <p><img alt="sa-2022-04-21-007" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-28-008.png" loading="lazy" class="lazy"></p> <p><strong>好了，今天就到这儿吧，我是冰河，我们下期见~~</strong></p> <h2 id="写在最后"><a href="#写在最后" class="header-anchor">#</a> 写在最后</h2> <blockquote><p>如果你觉得冰河写的还不错，请微信搜索并关注「 <strong>冰河技术</strong> 」微信公众号，跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术，「 <strong>冰河技术</strong> 」微信公众号更新了大量技术专题，每一篇技术文章干货满满！不少读者已经通过阅读「 <strong>冰河技术</strong> 」微信公众号文章，吊打面试官，成功跳槽到大厂；也有不少读者实现了技术上的飞跃，成为公司的技术骨干！如果你也想像他们一样提升自己的能力，实现技术能力的飞跃，进大厂，升职加薪，那就关注「 <strong>冰河技术</strong> 」微信公众号吧，每天更新超硬核技术干货，让你对如何提升技术能力不再迷茫！</p></blockquote> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200906013715889.png" loading="lazy" class="lazy"></p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/binghe001/BingheGuide/edit/master/docs/md/microservices/springcloudalibaba/2022-05-10-SA实战-第14章-服务网关-SpringCloudGateway核心技术.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">2022/5/23</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/md/microservices/springcloudalibaba/2022-05-08-SA实战-第13章-服务网关-项目整合SpringCloudGateway.html" class="prev">
          SA实战 ·《SpringCloud Alibaba实战》第13章-服务网关：项目整合SpringCloud Gateway网关
        </a></span> <span class="next"><a href="/md/microservices/springcloudalibaba/2022-05-11-SA实战-第15章-链路追踪-核心原理与解决方案.html">
          SA实战 ·《SpringCloud Alibaba实战》第15章-链路追踪：核心原理与解决方案
        </a>
        →
      </span></p></div> </main></div> <aside class="page-sidebar"> <div class="page-side-toolbar"><div class="option-box-toc-fixed"><div class="toc-container-sidebar"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:650px"><div style="font-weight:bold;text-align:center;">SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#本章总览" class="toc-sidebar-link">本章总览</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#本章概述" class="toc-sidebar-link">本章概述</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#网关断言" class="toc-sidebar-link">网关断言</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#springcloud-gateway内置断言" class="toc-sidebar-link">SpringCloud Gateway内置断言</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#演示内置断言" class="toc-sidebar-link">演示内置断言</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#自定义断言" class="toc-sidebar-link">自定义断言</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#网关过滤器" class="toc-sidebar-link">网关过滤器</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#局部过滤器" class="toc-sidebar-link">局部过滤器</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#全局过滤器" class="toc-sidebar-link">全局过滤器</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#网关熔断机制" class="toc-sidebar-link">网关熔断机制</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#关于星球" class="toc-sidebar-link">关于星球</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#星球提供的服务" class="toc-sidebar-link">星球提供的服务</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#星球门票价格" class="toc-sidebar-link">星球门票价格</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box-toc-over"><img src="/images/system/toc.png" class="nozoom"> <span class="show-txt">目录</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:550px"><div style="font-weight:bold;text-align:center;">SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#本章总览" class="toc-sidebar-link">本章总览</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#本章概述" class="toc-sidebar-link">本章概述</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#网关断言" class="toc-sidebar-link">网关断言</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#springcloud-gateway内置断言" class="toc-sidebar-link">SpringCloud Gateway内置断言</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#演示内置断言" class="toc-sidebar-link">演示内置断言</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#自定义断言" class="toc-sidebar-link">自定义断言</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#网关过滤器" class="toc-sidebar-link">网关过滤器</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#局部过滤器" class="toc-sidebar-link">局部过滤器</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#全局过滤器" class="toc-sidebar-link">全局过滤器</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#网关熔断机制" class="toc-sidebar-link">网关熔断机制</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#关于星球" class="toc-sidebar-link">关于星球</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#星球提供的服务" class="toc-sidebar-link">星球提供的服务</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#星球门票价格" class="toc-sidebar-link">星球门票价格</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box"><img src="/images/system/wechat.png" class="nozoom"> <span class="show-txt">手机看</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.9rem">微信扫一扫</span> <img height="180px" src="https://api.qrserver.com/v1/create-qr-code/?data=https://binghe001.github.io/md/microservices/springcloudalibaba/2022-05-10-SA%E5%AE%9E%E6%88%98-%E7%AC%AC14%E7%AB%A0-%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3-SpringCloudGateway%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF.html" style="margin:10px;">
                可以<b>手机看</b>或分享至<b>朋友圈</b></div></div></div></div> <div class="option-box"><img src="/images/system/toggle.png" width="30px" class="nozoom"> <span class="show-txt">左栏</span></div> <div class="option-box"><img src="/images/system/xingqiu.png" width="25px" class="nozoom"> <span class="show-txt">星球</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">实战项目<span style="font-size:8px;color:red;">「SpringCloud Alibaba实战项目」</span>、专属电子书、问题解答、简历指导、技术分享、晋升指导、视频课程</span> <img height="180px" src="/images/personal/xingqiu.png" style="margin:10px;"> <b>知识星球</b>：冰河技术
            </div></div></div></div> <div class="option-box"><img src="/images/system/wexin4.png" width="25px" class="nozoom"> <span class="show-txt">读者群</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">添加冰河微信<span style="color:red;">(hacker_binghe)</span>进冰河技术学习交流圈「无任何套路」</span> <img src="/images/personal/hacker_binghe.jpg" height="180px" style="margin:10px;">
                PS：添加时请备注<b>读者加群</b>，谢谢！
              </div></div></div></div> <div class="option-box"><img src="/images/system/download-2.png" width="25px" class="nozoom"> <span class="show-txt">下资料</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">扫描公众号，回复<span style="color:red;">“1024”</span>下载<span style="color:red;">100GB+</span>学习技术资料、PDF书籍、实战项目、简历模板等「无任何套路」</span> <img src="/images/personal/qrcode.png" height="180px" style="margin:10px;"> <b>公众号:</b> 冰河技术
              </div></div></div></div> <div class="option-box"><img src="/images/system/heart-1.png" width="25px" class="nozoom"> <span class="show-txt">赞赏我</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">鼓励/支持/赞赏我</span> <img height="180px" src="/images/personal/encourage-head.png" style="margin:5px;"> <br>1. 不靠它生存但仍希望得到你的鼓励；
                <br>2. 时刻警醒自己保持技术人的初心；
              </div></div></div></div> <div title="SA实战 ·《SpringCloud Alibaba实战》第13章-服务网关：项目整合SpringCloud Gateway网关" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/microservices/springcloudalibaba/2022-05-08-SA实战-第13章-服务网关-项目整合SpringCloudGateway.html"><img src="/images/system/pre2.png" width="30px" class="nozoom"> <span class="show-txt">上一篇</span></a></div> <div title="SA实战 ·《SpringCloud Alibaba实战》第15章-链路追踪：核心原理与解决方案" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/microservices/springcloudalibaba/2022-05-11-SA实战-第15章-链路追踪-核心原理与解决方案.html"><img src="/images/system/next2.png" width="30px" class="nozoom"> <span class="show-txt">下一篇</span></a></div></div>  <!----> </aside></div><div class="global-ui"><div class="read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="read-more-btn" target="_self" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">阅读全文</a> <div id="btw-modal-wrap" style="display: none;"><div id="btw-mask" style="position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; opacity: 0.7; z-index: 999; background: rgb(0, 0, 0);"></div> <div id="btw-modal" style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; text-align: center; font-size: 13px; background: rgb(255, 255, 255); border-radius: 10px; z-index: 9999; font-family: PingFangSC-Regular, sans-serif;"><span id="btw-modal-close-btn" style="position: absolute; top: 5px; right: 15px; line-height: 34px; font-size: 34px; cursor: pointer; opacity: 0.2; z-index: 9999; color: rgb(0, 0, 0); background: none; border: none; outline: none;">×</span> <p id="btw-modal-header" style="margin-top: 40px; line-height: 1.8; font-size: 13px;">
                扫码或搜索：<span style="color: #E9405A; font-weight: bold;">冰河技术</span> <br>发送：<span id="fustack-token" class="token" style="color: #e9415a; font-weight: bold; font-size: 17px; margin-bottom: 45px;">290992</span> <br>即可<span style="color: #e9415a; font-weight: bold;">立即永久</span>解锁本站全部文章</p> <img src="/images/personal/qrcode.png" style="width: 180px; margin-top: 10px; margin-bottom: 30px; border: 8px solid rgb(230, 230, 230);"></div></div></div><div class="pay-read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="pay-read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="pay-read-more-btn" target="_blank" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">付费阅读</a></div></div></div>
    <script src="/assets/js/cg-styles.js?v=1653305936337" defer></script><script src="/assets/js/cg-4.js?v=1653305936337" defer></script><script src="/assets/js/cg-3.js?v=1653305936337" defer></script><script src="/assets/js/cg-302.js?v=1653305936337" defer></script><script src="/assets/js/cg-5.js?v=1653305936337" defer></script><script src="/assets/js/cg-6.js?v=1653305936337" defer></script><script src="/assets/js/cg-app.js?v=1653305936337" defer></script>
  </body>
</html>
